Component({
  properties: {
    device: {
      type: Object,
      value: {

      }
    },

  },

  data: {
    show: false,
    deviceTypeIndex: 0,
    wineTypeIndex: 0,
    amount: 0,
    currentWineType: {},
    currentStock: '0'
  },

  methods: {
    showPopup() {
      this.setData({ 
        show: true,
        wineTypeIndex: 0,
        deviceTypeIndex:0,
        wineTypes:this.data.device.attrs,
        amount: 0,
        currentWineType: this.data.device.attrs[0],
        currentStock: this.data.device.attrs[0]?.attrStock,
        selectedDevice: this.data.device,
      });
    },

    onClose() {
      this.setData({ show: false });
    },

    onCancel() {
      this.onClose();
      this.triggerEvent('cancel');
    },

    handleWineTypeChange(e) {
      const index = e.detail.value;
      const selected = this.data.device.attrs[index];
      this.setData({
        wineTypeIndex: index,
        currentWineType: selected,
        currentStock: selected.attrStock
      });
    },


    decreaseAmount() {
      if (this.data.amount > 0) {
        this.setData({ amount: this.data.amount - 1 });
      }
    },

    increaseAmount() {
      this.setData({ amount: this.data.amount + 1 });
    },

    onConfirm() {
      let _this=this
      const { currentWineType, amount } = this.data;
      wx.showModal({
        title: '提示',
        content: `是否向${this.data.device.deviceName}设备添加${amount}斤${currentWineType.attrName}?`,
        success (res) {
          if (res.confirm) {
            _this.triggerEvent('confirm', {
              wineType: currentWineType.attrId,
              amount: amount,
              deviceName:_this.data.device.deviceName
            });
            _this.onClose();
          } else if (res.cancel) {
     
          }
        }
      })
      
    }
  }
});